home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / text / misc / mpage.lha / mpage / glob.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-25  |  15.7 KB  |  405 lines

  1. /*
  2.  * glob.c
  3.  */
  4.  
  5. /*
  6.  * mpage:    a program to reduce pages of print so that several pages
  7.  *           of output appear on one printed page.
  8.  *
  9.  * Written by:
  10.  *   ...!uunet!\                       Mark Hahn, Sr Systems Engineer
  11.  *              >pyrdc!mark            Pyramid Technology Corporation
  12.  * ...!pyramid!/                       Vienna, Va    (703)848-2050
  13.  *
  14.  *
  15.  * Copyright (c) 1988 Mark P. Hahn, Herndon, Virginia
  16.  * Copyright (c) 1994-1997 Marcel J.E. Mol, The Netherlands
  17.  *                    marcel@mesa.nl
  18.  *  
  19.  *     Permission is granted to anyone to make or distribute verbatim
  20.  *     copies of this document as received, in any medium, provided
  21.  *     that this copyright notice is preserved, and that the
  22.  *     distributor grants the recipient permission for further
  23.  *     redistribution as permitted by this notice.
  24.  *
  25.  */
  26.  
  27. #include "mpage.h"
  28.  
  29. /*
  30.  * to turn on debugging, define the preprocessor macro DEBUG and set
  31.  * the variable Debug_flag to the sum of the sections to debug.
  32.  */
  33. # ifdef DEBUG
  34. int Debug_flag = DB_PSMPAGE;
  35. # endif
  36.  
  37.  
  38. /*
  39.  * some basic PS parameters
  40.  */
  41. int ps_width;    /* number of points in the X direction see set_page() */
  42. int ps_height;    /* number of points in the Y direction */
  43. char *media;    /* name of output media */
  44.  
  45. /*
  46.  * the structures describe where to put the reduced pages of output on the
  47.  * printed page.
  48.  */
  49. /* empty page */
  50. struct pagepoints points_empty[] = {
  51.     {  0,  0,  0 }
  52. };
  53. /* base point for one page, normal aspect */
  54. struct pagepoints one_normal[] = {
  55.     { xbase1, ybase1,  0 },
  56.     {  0,  0,  0 }
  57. };
  58. /* base points for two pages, normal aspect */
  59. struct pagepoints two_normal[] = {
  60.     { xbase1, ytop4,  0 },    { xbase1 , ytop2,  0 },
  61.     {  0,  0,  0 }
  62. };
  63.  
  64. /* GPN outside 2 pages */
  65. struct pagepoints two_normal_co[] =   {
  66.        {xbase1, ytop2, 0},    {0, 0, 1},
  67.        {0,  0,  1},        {xbase1, ytop4, 0},
  68.        {0,  0,  0}
  69.  
  70. };
  71. /* GPN. inside 2 pages */
  72. struct pagepoints two_normal_ci[] =   {
  73.        {0, 0, 1},        {xbase1, ytop4, 0},
  74.        {xbase1, ytop2, 0},    {0, 0, 1},
  75.        {0,  0,  0}
  76. };
  77.  
  78. /* base points for four pages, normal aspect, running reduced pages
  79.  * read from left to right */
  80. struct pagepoints lr_four_normal[] = {
  81.       { xbase1, ybase3,  0 },    { xbase2, ybase3,  0 },
  82.     { xbase1, ybase1,  0 },    { xbase2, ybase1,  0 },
  83.     {  0,  0,  0 }
  84. };
  85. /* base points for four pages, normal aspect, running reduced pages
  86.  * read from top to bottom (up/down) */
  87. struct pagepoints ud_four_normal[] = {
  88.       { xbase1, ybase3,  0 },    { xbase1, ybase1,  0 },
  89.     { xbase2, ybase3,  0 },    { xbase2, ybase1,  0 },
  90.     {  0,  0,  0 }
  91. };
  92. /* base points for four pages, normal aspect, running reduced pages
  93.  * read from left to right, adjusting for the fact that we have a landscape
  94.  * input */
  95. struct pagepoints land_lr_four_normal[] =
  96. {
  97.     { xbase1, ybase1,  0 }, { xbase1, ybase3,  0 },
  98.     { xbase2, ybase1,  0 }, { xbase2, ybase3,  0 },
  99.     { 0,  0,  0}
  100. };
  101. /* base points for four pages, normal aspect, running reduced pages
  102.  * read from top to bottom (up/down), adjusting for the fact that we have a
  103.  * landscape input */
  104. struct pagepoints land_ud_four_normal[] =
  105. {
  106.     { xbase1, ybase1,  0 }, { xbase2, ybase1,  0 },
  107.     { xbase1, ybase3,  0 }, { xbase2, ybase3,  0 },
  108.     { 0,  0,  0}
  109. };
  110. /* base points for eight pages, normal aspect, running reduced pages
  111.  * read from left to right */
  112. struct pagepoints lr_eight_normal[] = {
  113.     { xbase2, ytop4,  0 },    { xbase2, ytop3,  0 },
  114.     { xbase2, ytop2,  0 },    { xbase2, ytop1,  0 },
  115.     { xbase1, ytop4,  0 },    { xbase1, ytop3,  0 },
  116.     { xbase1, ytop2,  0 },    { xbase1, ytop1,  0 },
  117.     {  0,  0,  0 }
  118. };
  119. /* base points for eight pages, normal aspect, running reduced pages
  120.  * read from top to bottom (up/down) */
  121. struct pagepoints ud_eight_normal[] = {
  122.     { xbase2, ytop4,  0 },    { xbase1, ytop4,  0 },
  123.     { xbase2, ytop3,  0 },    { xbase1, ytop3,  0 },
  124.     { xbase2, ytop2,  0 },    { xbase1, ytop2,  0 },
  125.     { xbase2, ytop1,  0 },    { xbase1, ytop1,  0 },
  126.     {  0,  0,  0 }
  127. };
  128. /* base points for eight pages, normal aspect, running reduced pages
  129.  * read from left to right, adjusting for the fact that we have a landscape
  130.  * input */
  131. struct pagepoints land_lr_eight_normal[] =
  132. {
  133.     { xbase1, ytop4,  0 },  { xbase2, ytop4,  0 },
  134.     { xbase1, ytop3,  0 },  { xbase2, ytop3,  0 },
  135.     { xbase1, ytop2,  0 },  { xbase2, ytop2,  0 },
  136.     { xbase1, ytop1,  0 },  { xbase2, ytop1,  0 },
  137.     { 0,  0,  0 }
  138. };
  139. /* base points for eight pages, normal aspect, running reduced pages
  140.  * read from top to bottom (up/down), adjusting for the fact that we have a
  141.  * landscape input */
  142. struct pagepoints land_ud_eight_normal[] =
  143. {
  144.     { xbase1, ytop4,  0 },  { xbase1, ytop3,  0 },
  145.     { xbase1, ytop2,  0 },  { xbase1, ytop1,  0 },
  146.     { xbase2, ytop4,  0 },  { xbase2, ytop3,  0 },
  147.     { xbase2, ytop2,  0 },  { xbase2, ytop1,  0 },
  148.     { 0,  0,  0}
  149. };
  150. /* base point for one page, in landscape */
  151. struct pagepoints one_landscape[] = {
  152.       { xbase1, ytop4,  0 },
  153.     {  0,  0,  0 }
  154. };
  155. /* base points for two pages, in landscape */
  156. struct pagepoints two_landscape[] = {
  157.       { xbase1, ybase3,  0 },    { xbase1, ybase1,  0 },
  158.     {  0,  0,  0 }
  159. };
  160. /* base points for four pages, in landscape, running reduced pages
  161.  * read from left to right */
  162. struct pagepoints lr_four_landscape[] = {
  163.       { xbase2, ytop4,  0 },    { xbase2, ytop2,  0 },
  164.     { xbase1, ytop4,  0 },    { xbase1, ytop2,  0 },
  165.     {  0,  0,  0 }
  166. };
  167. /* base points for four pages, in landscape, running reduced pages
  168.  * read from top to bottom (up/down) */
  169. struct pagepoints ud_four_landscape[] = {
  170.       { xbase2, ytop4,  0 },    { xbase1, ytop4,  0 },
  171.       { xbase2, ytop2,  0 },    { xbase1, ytop2,  0 },
  172.     {  0,  0,  0 }
  173. };
  174. /* base points for eight pages, in landscape, running reduced pages
  175.  * read from left to right */
  176. struct pagepoints lr_eight_landscape[] = {
  177.     { xbase1, ybase4,  0 },    { xbase2, ybase4,  0 },
  178.     { xbase1, ybase3,  0 },    { xbase2, ybase3,  0 },
  179.     { xbase1, ybase2,  0 },    { xbase2, ybase2,  0 },
  180.     { xbase1, ybase1,  0 },    { xbase2, ybase1,  0 },
  181.     {  0,  0,  0 }
  182. };
  183. /* base points for eight pages, in landscape, running reduced pages
  184.  * read from top to bottom (up/down) */
  185. struct pagepoints ud_eight_landscape[] = {
  186.     { xbase1, ybase4,  0 },    { xbase1, ybase3,  0 },
  187.     { xbase1, ybase2,  0 },    { xbase1, ybase1,  0 },
  188.     { xbase2, ybase4,  0 },    { xbase2, ybase3,  0 },
  189.     { xbase2, ybase2,  0 },    { xbase2, ybase1,  0 },
  190.     {  0,  0,  0 }
  191. };
  192.  
  193. /* list of sheets (printed page formats) for
  194.  * left to right reading, in normal aspect */
  195. struct sheet lr_normal[] = {
  196. /* 0 */    { 80, 66, xwid1, yht1,    0, outline_1, one_normal },
  197. /* 1 */    { 80, 66, yht2,  xwid1, -90, outline_2, two_normal },
  198. /* 2 */    { 80, 66, xwid2, yht2,    0, outline_4, lr_four_normal },
  199. /* 3 */    { 80, 66, yht4,  xwid2, -90, outline_8, lr_eight_normal },
  200. };
  201.  
  202. /* list of sheets (printed page formats) for landscape input
  203.  * left to right reading, in normal aspect */
  204. struct sheet land_lr_normal[] = {
  205. /* 0 */ { 80, 66, xwid1, yht1,    0, outline_1, one_normal },
  206. /* 1 */ { 80, 66, yht2,  xwid1, -90, outline_2, two_normal },
  207. /* 2 */ { 80, 66, xwid2, yht2,    0, outline_4, land_lr_four_normal },
  208. /* 3 */ { 80, 66, yht4,  xwid2, -90, outline_8, land_lr_eight_normal },
  209. };
  210.  
  211. /* list of sheets (printed page formats) for
  212.  * top to bottom reading, in normal aspect */
  213. struct sheet ud_normal[] = {
  214. /* 0 */    { 80, 66, xwid1, yht1,    0, outline_1, one_normal },
  215. /* 1 */    { 80, 66, yht2,  xwid1, -90, outline_2, two_normal },
  216. /* 2 */    { 80, 66, xwid2, yht2,    0, outline_4, ud_four_normal },
  217. /* 3 */    { 80, 66, yht4,  xwid2, -90, outline_8, ud_eight_normal },
  218. };
  219.  
  220. /* list of sheets (printed page formats) for
  221.  * left to right reading, in landscape */
  222. struct sheet lr_landscape[] = {
  223. /* 0 */    { 132, 52, yht1,  xwid1, -90, outline_1, one_landscape },
  224. /* 1 */    { 132, 52, xwid1, yht2,    0, outline_2, two_landscape },
  225. /* 2 */    { 132, 52, yht2,  xwid2, -90, outline_4, lr_four_landscape },
  226. /* 3 */    { 132, 52, xwid2, yht4,    0, outline_8, lr_eight_landscape },
  227. };
  228.  
  229. /* list of sheets (printed page formats) for
  230.  * top to bottom reading, in landscape */
  231. struct sheet ud_landscape[] = {
  232. /* 0 */    { 132, 52, yht1,  xwid1, -90, outline_1, one_landscape },
  233. /* 1 */    { 132, 52, xwid1, yht2,    0, outline_2, two_landscape },
  234. /* 2 */    { 132, 52, yht2,  xwid2, -90, outline_4, ud_four_landscape },
  235. /* 3 */    { 132, 52, xwid2, yht4,    0, outline_8, ud_eight_landscape },
  236. };
  237.           
  238. /* list of sheets (printed page formats) for landscape input
  239.  * top to bottom reading, in landscape */
  240. struct sheet land_ud_normal[] = {
  241. /* 0 */ { 80, 66, xwid1, yht1,    0, outline_1, one_normal },
  242. /* 1 */ { 80, 66, yht2,  xwid1, -90, outline_2, two_normal },
  243. /* 2 */ { 80, 66, xwid2, yht2,    0, outline_4, land_ud_four_normal },
  244. /* 3 */ { 80, 66, yht4,  xwid2, -90, outline_8, land_ud_eight_normal },
  245. };
  246.  
  247. /* GPN. sheet */
  248. struct sheet coli [] =   {
  249.       /* 1 */ { 80, 66, yht2,  xwid1, -90, outline_2, two_normal_co },
  250.       /* 1 */ { 80, 66, yht2,  xwid1, -90, outline_2, two_normal_ci },
  251. };
  252.  
  253. /* array of sheet lists for left to right reading printed pages */
  254. struct sheet *left_right[] = {
  255.     lr_normal,
  256.     lr_landscape,
  257.     land_lr_normal
  258.   };
  259.  
  260. /* arrays for top to bottom reading printed pages */
  261. struct sheet *up_down[] = {
  262.     ud_normal,
  263.     ud_landscape,
  264.     land_ud_normal
  265.   };
  266.  
  267. /*
  268.  * Variables for holding the chosen options,  The defaults are set here.
  269.  * the sheetlist pointer is set to point to the array for either up/down
  270.  * or left/right reading.  This array is index by sheetorder, and then
  271.  * sheetindex.  sheetindex encodes the number of reduced pages per printed
  272.  * page and indexes into the sheet list (0 = 1 page, 1 = two pages, 2 =
  273.  * four pages, 3 = eight pages).
  274.  */
  275. struct sheet **sheetlist;/* array of sheet lists (up/down or left/right) */
  276. int sheetaspect = PORTRAIT;        /* either normal or landscape */
  277. int sheetorder = UPDOWN;        /* up/down or left/right flag */
  278. int sheetindex = 2;    /* index to number of pages of output per printed */
  279. int sheetmargin_left  = DEFAULTSMARGIN;/* non-printable border on sheet */
  280. int sheetmargin_right = DEFAULTSMARGIN;/* non-printable border on sheet */
  281. int sheetmargin_top   = DEFAULTSMARGIN;/* non-printable border on sheet */
  282. int sheetmargin_bottom= DEFAULTSMARGIN;/* non-printable border on sheet */
  283. int pagemargin_left   = DEFAULTPMARGIN;/* border for pages */
  284. int pagemargin_right  = DEFAULTPMARGIN;/* border for pages */
  285. int pagemargin_top    = DEFAULTPMARGIN;/* border for pages */
  286. int pagemargin_bottom = DEFAULTPMARGIN;/* border for pages */
  287. int textmargin_left   = DEFAULTTMARGIN;/* border for textbox */
  288. int textmargin_right  = DEFAULTTMARGIN;/* border for textbox */
  289. int textmargin_top    = DEFAULTTMARGIN;/* border for textbox */
  290. int textmargin_bottom = DEFAULTTMARGIN;/* border for textbox */
  291. int sheetheader_left  = 0;   /* space for physical sheetheader */
  292. int sheetheader_right = 0;   /* space for physical sheetheader */
  293. int sheetheader_top   = 0;   /* space for physical sheetheader */
  294. int sheetheader_bottom= 0;   /* space for physical sheetheader */
  295. struct pagepoints *points = points_empty;
  296. int orientation;            /* final orientation of text */
  297. int fsize = TSIZE;            /* font scale size */
  298. int opt_indent = 0;            /* starting column for ascii printing */
  299. int opt_tabstop = DEFAULTTABSTOP;    /* width of a tab */
  300. int opt_lines = 0;        /* number of lines to fit on an reduced page */
  301. int opt_width = 0;    /* number of columns to fit on an reduced page */
  302. int opt_last = MAXINT;    /* print as many as supplied */
  303. int opt_page = PAGE_DEF;/* default paper size */
  304. /* boolean's: set default to 0 or 1 */
  305. int opt_pr = 0;        /* if true use pr(1) to format output */
  306. int opt_mp_header = 0;  /* let mpage create headers */
  307. int opt_sheetheader = 0;/* let mpage create sheetheaders */
  308. int opt_fold = 0;    /* fold lines longer than page width */
  309. int opt_outline = 1;    /* don't normally outline the pages */
  310. int opt_verbose = 0;    /* by default, print a count of pages produced */
  311. int opt_square = 1;    /* by default print pages with natural aspect ratio */
  312. int opt_reverse = 0;    /* by default print sheets in forward order */
  313. int opt_first = 1;    /* start with first sheet */
  314. int opt_alt = 1;    /* by default print all sheets, odd+even */
  315. int opt_file = 1;       /* should each file appera on a new sheet */
  316. int opt_duplex = 0;     /* duplex mode flag, OFF by default */
  317. int opt_tumble = 0;     /* tumble overy second pages, OFF by default */
  318. int opt_textbox = 0;    /* don't normally draw box around text */
  319. int opt_input = IN_AUTO;/* select input file format */
  320. int opt_encoding = DEFAULT_ENCODING;    /* use default encoding or not */
  321. struct pagebox textbox = {0, 0, 80, 66, 0};
  322.  
  323. char * opt_header = NULL;    /* the header for pr's -h option */
  324. char * printque = NULL;    /* the printer queuename */
  325. char * prprog = PRPROG;    /* the pr filter program */
  326. char * printprog = PRINTPROG; /* the print program */
  327. char * printarg = QUEARG; /* define print queue to printprog */
  328. int doprint = 0;    /* send it to the printer */
  329. char * charvec_file;    /*  file to read character definitions from */
  330. char * libdir = LIBDIR;    /* pointer to get library files from */
  331. char * fontname = "Courier";    /* Font to use */
  332. char * dateformat = "%c";    /* Date/time format for date in headers */
  333. char * sheethead = "";    /* Leave empty to get default sheetheader:
  334.                                     current filename + physical pagenumber */
  335.  
  336. /*
  337.  * various global information
  338.  */
  339. char MPAGE[] = "mpage";    /* program name */
  340. int ps_pagenum = 0;    /* current sheet count */
  341. int had_ps = 0;         /* did we process ps files */
  342.  
  343. int first_encoding = -1; /* first encoding in character set */
  344. int last_encoding;    /* last encoding in character set */
  345.  
  346. /* GPN. for coli */
  347. int Coli = 0;           /* value of 0=don't mess, 1 = 4,1 (outside pages), */
  348.  
  349.  
  350. void usage(int errcode)
  351. {
  352.     fprintf(stderr, "\
  353.  mpage - print multiple pages on postscript, version %s\n\n\
  354.  mpage [-acflortvEOHRST1248] [-d(a|p)] [-B[textboxmargins]]\n\
  355.        [-m[sheetmargins]] [-M[pagemargins]] [-p[prprog]] [-j<pages>]\n\
  356.        [-Iindent] [-Llines] [-Wwidth] [-hheader] [-P[printque]]\n\
  357.        [-Fpsfontname] [-C[encodingfile]] [-zprintcommand] [-Zqueuearg]\n\
  358.        [-Ddateformat] [-stabstop] -X[header]] [-bpapersize] [files...]\n",
  359.  VERSION
  360. );
  361.  
  362.     fprintf(stderr, "\n\
  363.  -1, -2, -4, -8  Pages per sheet (4)    -D strftime format for date specs\n\
  364.  -da Force ascii input format           -dp Force postscript input format\n\
  365.  -a Toggle across/updown layout (u)    -l Toggle portrait/landscape (p)\n\
  366.  -f Toggle folding long lines (off)    -o Toggle printing outlines (on)\n\
  367.  -r Reverse printing, last->first sheet    -v Toggle verbose output, (on)\n\
  368.  -F Text font to be used (Courier)      -C Character encoding filename\n\
  369.  -E Print every second and third page   -O Print every first and fourth page\n\
  370.  -b papersize: A4|A3|Letter|Legal (default %s)\n\
  371.  -s Define tabstop width (default %d)\n\
  372.  -R Switch to across mode, with first page at lower left corner\n\
  373.  -H Create page header for each page (text files only)\n\
  374.  -X Print physical page header (default filename + physical pagenumber)\n\
  375.  -c Toggle concat pages of different files on same sheet (off)\n\
  376.  -S Don't square pages (default uniform X/Y shrink, postscript files only)\n\
  377.  -B Specify textbox margin/thickness (no space): [<num>[lrtb]*]*\n\
  378.  -m Specify sheetmargin (no space): [<num>[lrtb]*]*\n\
  379.  -M Specify pagemargins (no space): [<num>[lrtb]*]*\n\
  380.  -p Pipe through prprog (no space), pr(1) is default.\n\
  381.     Mpage assumes the specified pr understands -l, -w and -h options.\n\
  382.  -P Specify printer queue (no space). -P only uses default queue.\n\
  383.  -z Specify print command (%s).\n\
  384.  -Z Specify print command print queue option (%s).\n\
  385.  -j Print specified sheets: -j first[-last][%%interval]\n\
  386.     -j 1-10 does first 10 sheets, -j 1%%2 prints odd ones, -j 2%%2 even ones.\n\
  387.  -t Toggle printing both sides of the paper (Duplex mode, off)\n\
  388.  -T Toggle tumble of every second pages when printing in duplex mode (off)\n",
  389. #if   PAGE_DEF == PAGE_A4
  390.         "A4"
  391. #elif PAGE_DEF == PAGE_LETTER
  392.         "US letter"
  393. #elif PAGE_DEF == PAGE_LEGAL
  394.         "US legal"
  395. #else
  396.         "unknown???"
  397. #endif
  398.     , opt_tabstop, printprog, printarg
  399.     );
  400.     fprintf(stderr, "\n(c) 1993-1997 Marcel Mol, marcel@mesa.nl (MESA Consulting)\n");
  401.  
  402.     return;
  403.  
  404. } /* usage */
  405.